using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.Service.IOCExtensions
{
	/// <summary>
	/// This is an extension class for Order DTO objects and lists
	/// </summary>
	public static partial class OrderExtension
	{
		#region RunSelect
		/// <summary>
		/// Select all objects from store.
		/// </summary>
		public static void RunSelect(this List<OrderDTO> list)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			list.AddRange(proxy.RunSelect());
		}
		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a paged result set.
		/// </summary>
		public static PagedQueryResults<OrderDTO> RunSelect(this List<OrderDTO> list, int page, int pageSize, bool ascending, string sortColumn, string linq)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			PagedQueryResults<OrderDTO> retval = proxy.RunSelect(page, pageSize, ascending, sortColumn, linq);
			if (retval.GridData != null)
				list.AddRange(retval.GridData);
			return retval;
		}

		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a result set.
		/// </summary>
		public static void RunSelect(this List<OrderDTO> list, string linq)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			list.AddRange(proxy.RunSelect(linq));
		}

		#endregion

		#region Persist

		/// <summary>
		/// Persists this object to store.
		/// </summary>
		public static void Persist(this OrderDTO order)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			proxy.Persist(order);
		}

		/// <summary>
		/// Persists this collection to store.
		/// </summary>
		public static void Persist(this List<OrderDTO> orderList)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			proxy.Persist(orderList);
		}

		#endregion

		#region Order SelectUsingPK

		/// <summary>
		/// Select a single object from this collection by its primary key.
		/// </summary>
		public static bool SelectByPrimaryKey(this OrderDTO order, int orderid)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			IOrderService orderService = (IOrderService)proxy;
			var item = orderService.SelectByPrimaryKey(orderid);
			if (item == null) return false;
			CopyObject(item, order);
			return true;
		}

		#endregion

		#region Order Select By Foreign Key
		/// <summary>
		/// Get the related 'OrderDetail' DTO list
		/// </summary>
		public static OrderDetailDTO[] GetOrderDetailList(this OrderDTO item)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			IOrderService service = (IOrderService)proxy;
			return service.GetOrderDetailList(item).ToArray();
		}

		/// <summary>
		/// Get the related 'Customer' DTO object
		/// </summary>
		public static CustomerDTO GetCustomer(this OrderDTO item)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			IOrderService service = (IOrderService)proxy;
			return service.GetCustomerItem(item);
		}

		/// <summary>
		/// Get the related 'Employee' DTO object
		/// </summary>
		public static EmployeeDTO GetEmployee(this OrderDTO item)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			IOrderService service = (IOrderService)proxy;
			return service.GetEmployeeItem(item);
		}

		/// <summary>
		/// Get the related 'Shipper' DTO object
		/// </summary>
		public static ShipperDTO GetShipper(this OrderDTO item)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			IOrderService service = (IOrderService)proxy;
			return service.GetShipperItem(item);
		}


		#endregion

		#region Delete

		/// <summary>
		/// Delete a single object.
		/// </summary>
		public static void Delete(this OrderDTO order)
		{
			var proxy = ProxyFactory<OrderDTO>.GetProxy();
			IOrderService orderService = (IOrderService)proxy;
			orderService.Delete(order);
		}

		#endregion

		/// <summary>
		/// Clones one DTO to another.
		/// </summary>
		internal static void CopyObject(OrderDTO source, OrderDTO destination)
		{
			destination.ShipVia = source.ShipVia;
			destination.ShipRegion = source.ShipRegion;
			destination.ShipPostalCode = source.ShipPostalCode;
			destination.ShippedDate = source.ShippedDate;
			destination.ShipName = source.ShipName;
			destination.ShipCountry = source.ShipCountry;
			destination.ShipCity = source.ShipCity;
			destination.ShipAddress = source.ShipAddress;
			destination.RequiredDate = source.RequiredDate;
			destination.OrderId = source.OrderId;
			destination.OrderDate = source.OrderDate;
			destination.Freight = source.Freight;
			destination.EmployeeId = source.EmployeeId;
			destination.CustomerId = source.CustomerId;
		}

	}
}
